package com.ruoyi.web.controller.system;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.TourPackage;
import com.ruoyi.system.service.ITourPackageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("package")
public class TourPackageController {

    @Autowired
    private ITourPackageService packageService;

    @GetMapping("/list")
    public AjaxResult list() {
        try {
            List<TourPackage> packages = packageService.selectPackages();
            if(packages != null) {
                return AjaxResult.success(packages);
            }
            return AjaxResult.error("查询失败");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("操作失败");
        }
    }

    @PostMapping("/add")
    public AjaxResult add(@RequestBody TourPackage tourPackage) {
        try {
            int row = packageService.addPackage(tourPackage);
            if(row != 0) {
                int id = tourPackage.getId();
                return AjaxResult.success(id);
            }
            return AjaxResult.error("添加失败");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("操作失败");
        }
    }

    @PostMapping("/update")
    public AjaxResult update(@RequestBody TourPackage tourPackage) {
        try {
            int row = packageService.updatePackage(tourPackage);
            if(row != 0) {
                return AjaxResult.success();
            }
            return AjaxResult.error("更新失败");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("操作失败");
        }
    }

    @PostMapping("/del")
    public AjaxResult del(int packageId) {
        try {
            int row = packageService.delPackage(packageId);
            if(row != 0) {
                return AjaxResult.success();
            }
            return AjaxResult.error("删除失败");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("操作失败");
        }
    }

    @PostMapping("/detail")
    public AjaxResult detail(int packageId) {
        try {
            TourPackage tourPackage = packageService.showPackage(packageId);
            if(tourPackage != null) {
                return AjaxResult.success(tourPackage);
            }
            return AjaxResult.error("查询失败");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("操作失败");
        }
    }
}
